package com.hz.sys.config;

import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.aop.Advisor;
import org.springframework.aop.aspectj.AspectJExpressionPointcut;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.TransactionDefinition;
import org.springframework.transaction.interceptor.*;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mcz
 * @Date 2018/10/25 18:01
 * @Description spring事物配置
 * @Version 1.0
 */
@Aspect
@Configuration
public class TransactionAdviceConfig_AOP {
    private Logger logger = LoggerFactory.getLogger(TransactionAdviceConfig_AOP.class);
    //事物超时时间
    private static final int TX_METHOD_TIMEOUT =20;
    //切点------》定义事物管理层 可以多个用||分开
    private static final String AOP_POINTCUT_EXPRESSION = "execution (* com.hz.user.service..*.*(..))||execution (* com.hz.user.interfaces..*.*(..))";
    @Autowired
    private PlatformTransactionManager transactionManager;
    
     /**
      * @Author mcz
      * @Description 定义事物规则
      * @Date 2018/10/29 13:34
      * @Param 
      * @return 
      **/
    @Bean
    public TransactionInterceptor txAdvice() {
        NameMatchTransactionAttributeSource source = new NameMatchTransactionAttributeSource();
        /* 只读事务，不做更新操作 */
        RuleBasedTransactionAttribute readOnlyTx = new RuleBasedTransactionAttribute();
        readOnlyTx.setReadOnly(true);
        readOnlyTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
        /* 当前存在事务就使用当前事务，当前不存在事务就创建一个新的事务 */
        RuleBasedTransactionAttribute requiredTx = new RuleBasedTransactionAttribute();
        requiredTx.setRollbackRules(Collections.singletonList(new RollbackRuleAttribute(Exception.class)));
        requiredTx.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRED);
        requiredTx.setTimeout(TX_METHOD_TIMEOUT);
        Map<String, TransactionAttribute> txMap = new HashMap<>(14);
        txMap.put("add*", requiredTx);
        txMap.put("create*", requiredTx);
        txMap.put("save*", requiredTx);
        txMap.put("insert*", requiredTx);
        txMap.put("update*", requiredTx);
        txMap.put("delete*", requiredTx);
        txMap.put("get*", readOnlyTx);
        txMap.put("query*", readOnlyTx);
        txMap.put("list*", readOnlyTx);
        txMap.put("select*", readOnlyTx);
        source.setNameMap(txMap);
        TransactionInterceptor txAdvice = new TransactionInterceptor(transactionManager, source);
        return txAdvice;
    }
   
     /**
      * @Author mcz
      * @Description  切点配置
      * @Date 2018/10/29 13:33
      * @Param 
      * @return 
      **/
    @Bean
    public Advisor txAdviceAdvisor() {
        DefaultPointcutAdvisor advisor=null;
        try {
            AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
            pointcut.setExpression(AOP_POINTCUT_EXPRESSION);
            advisor=new DefaultPointcutAdvisor(pointcut,txAdvice());
        } catch (Exception e) {
            logger.error("SpringBoot事物配置失败{}",e.getMessage());
        }
        logger.info("****************************SpringBoot事物配置成功****************************");
        return advisor;
        // return new DefaultPointcutAdvisor(pointcut, txAdvice);
    }
}

